/**
 * 
 */
package org.thymeleaf.spring.support;

import org.apache.commons.lang3.StringUtils;
import org.thymeleaf.Arguments;
import org.thymeleaf.Configuration;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;

/**
 * @author <a href="mailto:daonan.zhan@gmail.com">Joshua Zhan</a>
 *
 */
public class IdPrefixProcessor extends AbstractSingleAttributeModifierAttrProcessor {

    public static final int ATTR_PRECEDENCE = 2000;
    public static final String ATTR_NAME = "idPre";
    public static final String TARGET_ATTR_NAME = "id";

	public IdPrefixProcessor() {
		super(ATTR_NAME);
	}


	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.AbstractProcessor#getPrecedence()
	 */
	@Override
	public int getPrecedence() {
		return ATTR_PRECEDENCE;
	}


	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor#getTargetAttributeName(org.thymeleaf.Arguments, org.thymeleaf.dom.Element, java.lang.String)
	 */
	@Override
	protected String getTargetAttributeName(Arguments arguments, Element element, String attributeName) {
		return TARGET_ATTR_NAME;
	}


	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor#getTargetAttributeValue(org.thymeleaf.Arguments, org.thymeleaf.dom.Element, java.lang.String)
	 */
	@Override
	protected String getTargetAttributeValue(Arguments arguments, Element element, String attributeName) {
        final String attributeValue = element.getAttributeValue(attributeName);
        final String oldAttributeValue = element.getAttributeValue(TARGET_ATTR_NAME);
        
        if (StringUtils.isBlank(oldAttributeValue)) {
        	return null;
        }

        final Configuration configuration = arguments.getConfiguration();
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);

        final IStandardExpression expression = expressionParser.parseExpression(configuration, arguments, attributeValue);

        final Object result = expression.execute(configuration, arguments);
        return (result == null? "" : result.toString().concat(oldAttributeValue));
	}


	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor#getModificationType(org.thymeleaf.Arguments, org.thymeleaf.dom.Element, java.lang.String, java.lang.String)
	 */
	@Override
	protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName,
			String newAttributeName) {
		// TODO Auto-generated method stub
		return ModificationType.SUBSTITUTION;
	}

	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor#removeAttributeIfEmpty(org.thymeleaf.Arguments, org.thymeleaf.dom.Element, java.lang.String, java.lang.String)
	 */
	@Override
	protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName,
			String newAttributeName) {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor#recomputeProcessorsAfterExecution(org.thymeleaf.Arguments, org.thymeleaf.dom.Element, java.lang.String)
	 */
	@Override
	protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
		return false;
	}

}
